package com.kzd.gtboon.main.home

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ConvertUtils
import com.kzd.gtboon.R
import com.kzd.gtboon.custom.createViewTextMove
import com.kzd.gtboon.custom.setTransitionStatusE
import com.kzd.gtboon.databinding.IncludeAppRefreshBinding
import com.kzd.gtboon.entity.GameBean
import com.kzd.gtboon.jetpack.vm.SearchViewModel
import com.kzd.gtboon.main.game.adapter.Game1Adapter
import com.kzd.gtboon.main.home.adapter.HomeSearchGuessAdapter
import com.kzd.gtboon.main.toGameDetailsActivity
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class SearchGameFragment : OwnerViewBindingFragment<IncludeAppRefreshBinding>() {

    companion object {
        fun newInstance(): SearchGameFragment {
            return SearchGameFragment()
        }
    }

    private val mGame1Adapter = Game1Adapter<GameBean>()
    private val mHomeSearchGuessAdapter = HomeSearchGuessAdapter()

    private val mViewModel:SearchViewModel by lazy {
        ownerViewModel(requireActivity())
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> IncludeAppRefreshBinding
        get() = IncludeAppRefreshBinding::inflate

    override fun initView() {

        mViewBinding.rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(requireContext())
        }.adapter = mGame1Adapter

        mGame1Adapter.setOnItemClickListener { _, _, position ->
            val gid = mGame1Adapter.getItem(position).id
                ?: return@setOnItemClickListener
            mViewModel.searchClick(gameId = gid)
        }

        mViewBinding.mSmartRefreshLayout.setOnRefreshListener {
            val keyword = mViewModel.keywordLiveData.value
            if (keyword.isNullOrEmpty()) {
                mViewBinding.mSmartRefreshLayout.finishRefresh()
            } else {
                mViewModel.keywordLiveData.postValue(keyword)
            }
        }
    }

    override fun initViewMode() {
        mViewModel.searchInfoLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response,
                refresh = mViewBinding.mSmartRefreshLayout,
                isEnableLoadMore = (false))
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data

            if (data.isNullOrEmpty()) {
                mGame1Adapter.setHeaderView(createViewTextMove(requireContext(),
                    text = "找不到游戏？看看这边有没有你要的游戏吧",
                    height = ConvertUtils.dp2px(100f), textColor = R.color.FF_999999))

                mGame1Adapter.setFooterView(RecyclerView(requireContext()).apply {
                    layoutManager = LinearLayoutManager(requireContext()).apply {
                        orientation = LinearLayoutManager.HORIZONTAL
                    }
                    adapter = mHomeSearchGuessAdapter.apply {
                        val guess = mViewModel.searchDefaultLiveData.value
                            ?.data?.data ?: return@observe
                        setNewInstance(guess)
                    }
                })
            } else {
                mGame1Adapter.removeAllHeaderView()
                mGame1Adapter.removeAllFooterView()
                mGame1Adapter.setNewInstance(data)
            }
        })

        mViewModel.searchClickLiveData.observe(viewLifecycleOwner, { response ->
            if (response == null) {
                return@observe
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val gameId = response.data?.gameId
                ?: return@observe
            toGameDetailsActivity(gameId)
        })
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mGame1Adapter.removeAllHeaderView()
        mGame1Adapter.removeAllFooterView()
        mGame1Adapter.data.clear()
        mGame1Adapter.notifyDataSetChanged()
        mViewModel.searchClickLiveData.postValue(null)
    }

}